//
//  SMTextView.m
//  TengBean
//
//  Created by apple on 2017/4/12.
//  Copyright © 2017年 zsr. All rights reserved.
//

#import "SMTextView.h"
#define ISVersion9 [[UIApplication sharedApplication].version float] < 9.0
#ifdef ISVersion9
#define KNFont(s) [UIFont systemFontOfSize:s]
#else
#define KNFont(s) [UIFont fontWithName:@"PingFangSC-Regular" size:s]
//#define KNFont(s) [UIFont fontWithName:@"MicrosoftYaHei" size:s]
#endif
#define UIColorFromRGBAlph(rgbValue,alpah) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:alpah]
#define KMainBlack UIColorFromRGBAlph(0x000000, 1)

#define KScreenW [UIScreen mainScreen].bounds.size.width
#define KScreenH [UIScreen mainScreen].bounds.size.height

@interface SMTextView()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *placehoderLabel;
@property (nonatomic, strong) UILabel *limitConntLabel;
@property (nonatomic, assign) NSInteger limitCount;

@end

@implementation SMTextView

- (instancetype)initWithPlacehoder:(NSString *)placehoder fontSize:(CGFloat)fontSize limitCout:(NSInteger)limitCount frame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 180)];
        [self addSubview:self.textView];
        //__weak weakSelf = self;
        CGSize size = frame.size;
        self.textView.frame = CGRectMake(0, 0, size.width, size.height - 29);
        
        self.textView.textContainerInset = UIEdgeInsetsMake(19, 10, 25, 10);
        self.textView.font = KNFont(fontSize);
        self.textView.textColor = KMainBlack;
        self.limitCount = limitCount;
        self.textView.delegate = self;
        _placehoderLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, 20, 200, fontSize)];
        [self addSubview:_placehoderLabel];
        
        
//        _limitConntLabel =
//
//
//        [CreateUIView createLabelWithTextColor:KMainGray textAligment:NSTextAlignmentRight font:KNFont(fontSize) text:[NSString stringWithFormat:@"0/%ld",limitCount]];
//        [self addSubview:_limitConntLabel];
//        [_limitConntLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(-15);
//            make.height.mas_equalTo(fontSize);
//            make.bottom.mas_equalTo(weakSelf).offset(-15);
//        }];
    }
    return self;
}



- (void)textViewDidChange:(UITextView *)textView{
    
    self.placehoderLabel.hidden = (textView.text.length != 0) ? YES:NO;
    // 键盘输入模式
    NSString *lang = textView.textInputMode.primaryLanguage;
    if([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        if (!position) {
            //NSLog(@"textString = %@",textString);
            [self setTextStyle:textView];
        }else{
            // 有高亮选择的字符串，则暂不对文字进行统计、限制等处理
        }
    }else{
        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        //NSLog(@"textString = %@",textString);
        [self setTextStyle:textView];
        
    }
}

- (void)setTextStyle:(UITextView *)textView{
    NSInteger length = textView.text.length;
    if(length > self.limitCount && self.limitCount != 0){
        NSString *text = [textView.text substringWithRange:NSMakeRange(0, self.limitCount)];
        length = self.limitCount;
        textView.text = text;
    }
    
    self.limitConntLabel.text = [NSString stringWithFormat:@"%ld/%ld", length, self.limitCount];


    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;// 字体的行间距
    NSDictionary *attributes = @{

                                 NSFontAttributeName:self.textView.font,
                                 NSParagraphStyleAttributeName:paragraphStyle

                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
    
    //值发生变化
    if(self.valueChangeBlock){
        self.valueChangeBlock(textView.text);
    }
}

- (NSString *)text{
    return _textView.text;
}

- (void)setText:(NSString *)text{
    self.textView.text = text;
    self.placehoderLabel.hidden = text.length > 0 ? YES:NO;
}

- (void)showHidenLimit{
    
    self.limitConntLabel.hidden = YES;
    
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    [super setBackgroundColor:backgroundColor];
    [self.textView setBackgroundColor:backgroundColor];
}

- (void)updateSize:(CGSize)size{
    CGRect frame =  self.textView.frame;
    frame.size = CGSizeMake(size.width, size.height - 29);
    self.textView.frame = frame;
}


@end
